package com.dev.dream.libs.slidemenu.manager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;

public class RetrievePackages {

	private Context _ctx;

	public RetrievePackages(Context ctx) {
		_ctx = ctx;
	}

	public ArrayList<PInfo> getPackages() {
		ArrayList<PInfo> apps = getInstalledApps(false); /*
														 * false = no system
														 * packages
														 */
		final int max = apps.size();
		for (int i = 0; i < max; i++) {
			apps.get(i).prettyPrint();
		}
		return apps;
	}

	public ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
		ArrayList<PInfo> res = new ArrayList<PInfo>();

		try {
			List<PInfo> list = new ArrayList<PInfo>();
			List<PackageInfo> packs = _ctx.getPackageManager()
					.getInstalledPackages(0);
			for (int i = 0; i < packs.size(); i++) {
				PackageInfo p = packs.get(i);
				if ((!getSysPackages) && (p.versionName == null)) {
					continue;
				}
				if (((p.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) != true) {
					PInfo newInfo = new PInfo();
					newInfo.appname = p.applicationInfo.loadLabel(
							_ctx.getPackageManager()).toString();
					newInfo.pname = p.packageName;
					newInfo.versionName = p.versionName;
					newInfo.versionCode = p.versionCode;
					newInfo.icon = p.applicationInfo.loadIcon(_ctx
							.getPackageManager());

					res.add(newInfo);
				}
			}

			PInfo temp;
			for (int i = 0; i < res.size() - 1; i++) {
				for (int j = i + 1; j < res.size(); j++) {
					if (res.get(i).appname.toLowerCase().charAt(0) > res.get(j).appname
							.toLowerCase().charAt(0)) {
						temp = res.get(i);
						res.set(i, res.get(j));
						res.set(j, temp);
					}
				}
			}
		} catch (Exception ex) {
		}
		return res;
	}
}
